<template>
	<view>
		<u-picker :show="storeShow" :columns="storeList" keyName="label" @confirm="confirm()" @cancel="cancel()"
			@close="close()" closeOnClickOverlay="true" :defaultIndex="[0]">
		</u-picker>
	</view>
</template>

<script>
	export default {
		name: "store",
		data() {
			return {
				storeShow: false,
				storeList: [
					[]
				],
				defaultMerchantStore: {
					value: '',
					label: '',
				},
				defaultMerchantStoreIndex: [0],
			};
		},
		created() {
			this.findAllMerchantStoreByUserId();
		},
		methods: {

			init(storeShow) {
				if (storeShow) {
					this.storeShow = true;
				} else {
					this.storeShow = false;
				}
			},

			confirm(e) {
				console.log(e.value)
				this.storeShow = false;
				this.$emit('storeData', e.value[0]);
				this.$emit('selectedFilter', 0);
			},
			cancel() {
				this.$emit('selectedFilter', 0);
				this.storeShow = false;
			},
			close() {
				this.$emit('selectedFilter', 0);
				this.storeShow = false;
			},

			findAllMerchantStoreByUserId() {
				this.storeList[0].length = 0;
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				this.storeList[0] = [{
					value: '-1',
					label: '不限',
				}];
				this.$api('merchantStore.findAllMerchantStoreByUserId').then(res => {
					if (res.status == 0) {

						for (let d of res.data) {
							this.storeList[0].push({
								value: d.id,
								label: this.truncateStoreName(d.storeName),
								defaultStatus: d.defaultStatus
							})
						}
						// console.log("测试", this.storeList)
						this.defaultMerchantStoreMethod();
						uni.hideLoading();
					} else {
						uni.hideLoading();
						this.$helpers.toast('请求数据失败:' + res.data.msg);
					}
				});
			},

			truncateStoreName(storeName) {
			    if (storeName.length >= 10) {
			        // 截取前4个字符并添加省略号
			        let truncated = storeName.substring(0, 4) + '...';
			        // 添加最后4个字符
			        truncated += storeName.substring(storeName.length - 4);
			        return truncated;
			    } else {
			        // 如果长度小于10，则返回原字符串
			        return storeName;
			    }
			},

			defaultMerchantStoreMethod(){
				try {
					for (let i = 0; i < this.storeList[0].length; i++) {
						const merchantStore = this.storeList[0][i];
						if (merchantStore.defaultStatus){
							this.defaultMerchantStoreIndex[0] = i;
							this.defaultMerchantStore.value = merchantStore.value;
							this.defaultMerchantStore.label = merchantStore.label;
							break;
						}
					}

					if (this.$u.test.isEmpty(this.defaultMerchantStore.value) && this.$u.test.isEmpty(this.defaultMerchantStore.label) && this.storeList[0].length >= 1){
						// 如果没有默认场地就默认第一个
						const merchantStore = this.storeList[0][0];
						this.defaultMerchantStoreIndex[0] = 0;
						this.defaultMerchantStore.value = merchantStore.value;
						this.defaultMerchantStore.label = merchantStore.label;
					}

					// console.log("defaultMerchantStoreIndex：")
					// console.log(this.defaultMerchantStoreIndex)
					// this.$emit('storeData', this.defaultMerchantStore);
				} catch (e) {
					console.error(e.toString())
				}
			},

		}
	}
</script>

<style>

</style>
